/**
 * Created by fangjiahui on 15/12/14.
 */

// 所有模块都通过 define 来定义
// header里面需要引入jquery
define(function (require, exports, module) {

    // 通过 require 引入依赖

    //default common setting
    var _option = {
    }

    var AjaxReturn = {
        _self: this, //if target self
        ajaxPacket: function (url, type, tplLoc, container) {
            $.ajax({
                url: url,
                type: type,
                dataType: "json",
                success: function (data) {
                    
                    var template = Handlebars.compile(tplLoc);
                    var html = template(data);
                    $(container).html(html);
                }
            });
        },
        ajaxWithHandlebar: function (url, tpl, targetElement, option) {//ajaxWithHandlebar("/indexBorrowInfo.html?type=usually", require("/themes/theme_default/media/tpl/home_invest.tpl"), $("#index_invest_list"));
            var option = $.extend({
                isAppend: true,
                data: {},
            }, _option,option);
            $.ajax({
                url: url,
                type: "get",
                dataType: "json",
                cache: false,
                success: function (data) {
                    var template = Handlebars.compile(tpl);
                    var newData = $.extend({}, data, option.data);
                    var html = template(newData);
                    if (option.isAppend) {
                        targetElement.append(html);
                    } else {
                        targetElement.html(html);
                    }
                    if(typeof option.callBackFunction === 'function') {
                        option.callBackFunction(data);
                    }
                }
            })
        },
        ajaxSimple: function (url, type, callBackFunction) {
            $.ajax({
                url: url,
                type: type,
                dataType: "json",
                cache: false,
                success: callBackFunction
            });
        },

    }


    // 通过 exports 对外提供接口
    //exports.doSomething = ...

    // 或者通过 module.exports 提供整个接口
    module.exports = AjaxReturn;

});